package controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import po.TbUser;
import service.TbUserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@Controller
public class Login {
	@Autowired
	TbUserService tbUserService;

	@RequestMapping("/login")
	public String login(@RequestParam("username") String username,HttpSession httpSession){
		httpSession.setAttribute("username", username);
		if(username!=null&&!username.equals("")) {
			return "main";
		}else{
			return "fail";
		}
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession httpSession){
		httpSession.removeAttribute("username");
		return "login";
	}
    @RequestMapping("/main")
    public String main(HttpSession httpSession){
        String username=(String)httpSession.getAttribute("username");
        if(username!=null&&!username.equals("")) {
            return "main";
        }else{
            return "fail";
        }
    }
	//文字会商
	@RequestMapping("/textmeet")
	public String textmeet(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		if(username!=null&&!username.equals("")) {
			return "textmeet";
		}else{
			return "fail";
		}
	}
	//视频会商
	@RequestMapping("/videomeet")
	public String videomeet(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		if(username!=null&&!username.equals("")) {
			return "videomeet";
		}else{
			return "fail";
		}
	}
	//协同标会
	@RequestMapping("/gismeet")
	public String gismeet(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		if(username!=null&&!username.equals("")) {
			return "gismeet";
		}else{
			return "fail";
		}
	}
	//标会工具
	@RequestMapping("/gislocal")
	public String gislocal(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		if(username!=null&&!username.equals("")) {
			return "gislocal";
		}else{
			return "fail";
		}
	}
	//现场图像
	@RequestMapping("/remotemonitor")
	public String remotemonitor(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		if(username!=null&&!username.equals("")) {
			return "remotemonitor";
		}else{
			return "fail";
		}
	}

	/**
	 * 登陆请求
	 */
	@RequestMapping("/ssologon")
	public String ssologon(Model model, HttpServletRequest request, HttpServletResponse response) {
		try{
			Subject currentUser = SecurityUtils.getSubject();

			String username = (String) currentUser.getSession().getAttribute("username");

			if (null != username&&!"".equals(username)) {
				return "forward:/main";
			}else{
				return "forward:/logout";
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return "forward:/logout";
	}
	/**
	 * 登出请求
	 */
	@RequestMapping("/ssologout")
	public String casLogout(HttpServletRequest request, HttpServletResponse response) {
		try{
			Subject currentUser = SecurityUtils.getSubject();
			Session session = currentUser.getSession();
			String admin = (String)session.getAttribute("username");
			SecurityUtils.getSubject().logout();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return "forward:/logout";
	}

	@RequestMapping("/getuser")
	@ResponseBody
	public TbUser getuser(HttpSession httpSession){
		String username=(String)httpSession.getAttribute("username");
		TbUser tbUser=tbUserService.getuserbyname(username);
		return tbUser;
	}
  }
